﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Windows.Forms;


namespace ChuyenDongVatLy
{
    public class GameMPN : Microsoft.Xna.Framework.GameComponent
    {
        Game1 game;
        BaseObject _vatthe;
        Vector2 S0 = new Vector2(260, 370);
        Vector2 S01 = new Vector2(100, 340);

        private Texture2D picDong;
        private Texture2D picTinh;
        private Texture2D picPlay;
        private Texture2D picStop;
        private Texture2D picPause;
        private Texture2D picControl;

        private Texture2D picPlay2;
        private Texture2D picStop2;
        private Texture2D picPause2;
        private Texture2D picControl2;
        private SpriteFont font1;
        private Texture2D picArrow;
        double _TanSoGocCLLX = Math.PI * 4;
        double _PhaBanDauCLLX = Math.PI;



        private bool _isPause = true;
        private bool _isMOBPlay = false;
        private bool _isMOBPause = false;
        private bool _isMOBStop = false;

        MouseGame mouse;
        float _f;
        float _heSoMaSat = 0.01f;
        Texture2D _thanhchan;
        float _vantocLast = 0;
        double _AngelOfF;


        float _X;
        float _Y;
        List<float> _GiaTocChange;
        List<float> _GiaTocChangeBack;
        float _giaTocBanDau = 0;       
        bool huongdichuyen;
        int numberchange;
        Vector2 _poschange;
        List<ThanhMPN> _listThanh;
        List<float> _listTime;
        List<float> _listTimeBack;
        List<float> _listV0;
        List<float> _listV0back;
        List<ThanhMPN> _listThanhDao;
        List<ThanhMPN> _listThanhDraw;
        List<Vector2> _PosGone;

        ThanhMPN _thanhMPN1;
        ThanhMPN _thanhMPN2;
        ThanhMPN _thanhMPN3;

        ThanhMPN _thanhMPN10;
        ThanhMPN _thanhMPN20;
        ThanhMPN _thanhMPN30;

        Geometry ge = new Geometry();
        private float Amplitude=100;
        BaseObject _vatthe2;
        private Texture2D _imageSprig;
        private float _thoiGian;
        float _vantocRoiMPN;
        
         private bool isDrawFly=false;

         private Vector2 Vitricuoi;
         private Texture2D picPosGone;
         private Texture2D picMatDat;
        public GameMPN(Game1 game1)
            : base(game1)
        {
            game = game1;
            mouse = new MouseGame(game);
            _listThanh = new List<ThanhMPN>();
            _listThanhDao = new List<ThanhMPN>();
            _listThanhDraw = new List<ThanhMPN>();

            _thanhMPN1 = new ThanhMPN(this.game, 0, 400, new Vector2(0, 400));
            _thanhMPN2 = new ThanhMPN(this.game, Math.PI / 6, 200, _thanhMPN1.PointEnd());
            _thanhMPN3 = new ThanhMPN(this.game,Math.PI/12, 200, _thanhMPN2.PointEnd());

            _thanhMPN10 = new ThanhMPN(this.game, 0, 400, new Vector2(0, 400));
            _thanhMPN20 = new ThanhMPN(this.game, Math.PI / 6, 200, _thanhMPN1.PointEnd());
            _thanhMPN30 = new ThanhMPN(this.game, Math.PI / 12, 200, _thanhMPN2.PointEnd());
            _listThanh.Add(_thanhMPN1);
            _listThanh.Add(_thanhMPN2);
            _listThanh.Add(_thanhMPN3);

            _listThanhDraw.Add(_thanhMPN10);
            _listThanhDraw.Add(_thanhMPN20);
            _listThanhDraw.Add(_thanhMPN30);

           
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanhDao.Add(null);
            }
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanhDao[i] = _listThanh[_listThanh.Count - i - 1];
            }                       
            _vatthe = new BaseObject();
            _vatthe.PosCenter = S0;
            _listTime = new List<float>();
            _listV0 = new List<float>();
            _listTimeBack = new List<float>();
            _listV0back = new List<float>();
            _GiaTocChange = new List<float>();
            _GiaTocChangeBack = new List<float>();
            for (int i = 0; i < 10; i++)
            {
                float x = 0;
                _listTime.Add(x);
                _listV0.Add(x);
                _listTimeBack.Add(x);
                _listV0back.Add(x);
                _GiaTocChange.Add(x);
                _GiaTocChangeBack.Add(x);
            }
            _listV0[0] = _vantocLast;
            _GiaTocChange[0] = _giaTocBanDau;
            _vatthe.V0 = _vantocLast;
            _vatthe.A = _GiaTocChange[0];
            //////////////////////////////           

            _vatthe2 = new BaseObject();
            _vatthe2.AngFre = _TanSoGocCLLX;
            _vatthe2.AnBegin = _PhaBanDauCLLX;
            _vatthe2.Pos = S01;
            ///////////        
            Vitricuoi = _thanhMPN3.PointEnd();
            _PosGone = new List<Vector2>();
            Timer TimerDraw = new Timer();
            TimerDraw.Enabled = true;
            TimerDraw.Interval = 100;
            TimerDraw.Tick += new EventHandler(TimerDraw_Tick);
        }

        void TimerDraw_Tick(object sender, EventArgs e)
        {
            if (isDrawFly)
                isDrawFly = false;
            else
                isDrawFly = true;
        }
        public override void Initialize()
        {
            base.Initialize();
        }

        public void LoadContent(ContentManager Content)
        {
            _thanhchan = Content.Load<Texture2D>("matphangnghieng");
            _vatthe.Picture = Content.Load<Texture2D>("ball");
            for (int i = 0; i < _listThanh.Count && _listThanh[i] != null; i++)
            {
                _listThanh[i].LoadContent(Content);
                _listThanhDraw[i].LoadContent(Content);
            }

            picDong = Content.Load<Texture2D>("MPN\\dong");
            picTinh = Content.Load<Texture2D>("MPN\\tinh");
            picControl = Content.Load<Texture2D>("Button\\b_Control");
            picPlay = Content.Load<Texture2D>("Button\\b_Play");
            picPause = Content.Load<Texture2D>("Button\\b_Pause");
            picStop = Content.Load<Texture2D>("Button\\b_Stop");

            picControl2 = Content.Load<Texture2D>("Button\\a_Control");
            picPlay2 = Content.Load<Texture2D>("Button\\a_Play");
            picPause2 = Content.Load<Texture2D>("Button\\a_Pause");
            picStop2 = Content.Load<Texture2D>("Button\\a_Stop");
            _imageSprig = Content.Load<Texture2D>("DDDH\\spring");
            _vatthe2.Picture = Content.Load<Texture2D>("ball");

            font1 = Content.Load<SpriteFont>("font");

            picArrow = Content.Load<Texture2D>("DDDH\\arrow");
            picPosGone = Content.Load<Texture2D>("bombfrag");
            picMatDat = Content.Load<Texture2D>("gach");
        }
        public override void Update(GameTime gameTime)
        {            
            if (CheckVaCham())
            {
                _GiaTocChange[0] = Math.Abs(_vatthe2.A);
                _vatthe.A = _GiaTocChange[0];
            }
            MouseOverButton();            
            CheckButton();
            Move(gameTime);
            MoveCLLX(gameTime);
            if (_vatthe.PosCenter.X>=Vitricuoi.X)
            {
                ChuyenDongNemXien(gameTime, _thanhMPN3.Angle, Vitricuoi, _vantocRoiMPN);
            }
            
            base.Update(gameTime);
        }
        public void Move(GameTime gameTime)
        {
            if (_isPause == false)
            {
                if (LineChangeV() != Vector2.Zero)
                {
                    _poschange = LineChangeV();
                }
                if (NumberOfLineChange() > 0)
                {
                    numberchange = NumberOfLineChange();
                }
                if (_vatthe.V >= 0)
                    huongdichuyen = true;
                else
                    huongdichuyen = false;
                for (int i = 0; i < _listThanh.Count; i++)
                {
                    if (CheckCollisionMPN(_listThanh[i], _vatthe))
                    {
                        if (huongdichuyen)
                        {
                            for (int j = 0; j < _listThanh.Count; j++)
                            {
                                if (j != i)
                                {
                                    _listTime[j] = 0;
                                    _listV0[j] = 0;
                                    _GiaTocChange[j] = 0;
                                }
                            }
                            float sx;
                            if (i == 0)
                            {
                                sx = S0.X;
                            }
                            else
                            {
                                sx = 0;
                            }
                            _vatthe.A = _GiaTocChange[i] - DeltaAcceleration(_listThanh[i]);                            
                            _GiaTocChange[i + 1] = _vatthe.A;
                            _listTime[i] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                            _X = (float)((_listV0[i] * _listTime[i] + 0.5 * _vatthe.A * _listTime[i] * _listTime[i]) * Math.Cos(_listThanh[i].Angle)) + _listThanh[i].Position.X + sx;
                            _Y = -(float)((_listV0[i] * _listTime[i] + 0.5 * _vatthe.A * _listTime[i] * _listTime[i]) * Math.Sin(_listThanh[i].Angle)) + _listThanh[i].Position.Y - ConstPhysic._R;
                            _vatthe.V = _listV0[i] + _vatthe.A * _listTime[i];
                            _listV0[i + 1] = _vatthe.V;
                            _AngelOfF = _listThanh[i].Angle;
                        }
                        else
                        {
                            for (int j = 0; j < _listThanhDao.Count; j++)
                            {
                                if (j != _listThanhDao.Count - i - 1)
                                {
                                    _listTimeBack[j] = 0;
                                    _listV0back[j] = 0;
                                    _GiaTocChangeBack[j] = 0;
                                }
                            }
                            int k = _listThanhDao.Count - i - 1;
                            if (i == numberchange)
                            {
                                _listTimeBack[k] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                                _X = (float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Cos(_listThanhDao[k].Angle)) + _poschange.X;
                                _Y = -(float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Sin(_listThanhDao[k].Angle)) + _poschange.Y;
                                _vatthe.V = _listV0back[k] + _vatthe.A * _listTimeBack[k];
                                _listV0back[k + 1] = _vatthe.V;
                                _GiaTocChangeBack[k + 1] = _vatthe.A;
                                _AngelOfF = _listThanh[i].Angle;
                            }
                            else
                            {
                                _vatthe.A = _GiaTocChangeBack[k] - DeltaAcceleration(_listThanhDao[k]);
                                _listTimeBack[k] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                                _GiaTocChangeBack[k + 1] = _vatthe.A;
                                _X = (float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Cos(_listThanhDao[k].Angle)) + _listThanhDao[k].PointEnd().X;
                                _Y = -(float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Sin(_listThanhDao[k].Angle)) + _listThanhDao[k].PointEnd().Y - ConstPhysic._R;
                                _listV0back[k + 1] = _vatthe.V;
                                _AngelOfF = _listThanh[i].Angle;
                            }
                        }
                        _vatthe.PosCenter = new Vector2(_X, _Y);
                        _vantocRoiMPN = _vatthe.V;
                    }                                       
                }                
            }
        }

        public bool CheckCollisionMPN(ThanhMPN thanh, BaseObject vatthe)
        {
            Geometry ge1 = new Geometry();
            if (ge1.DistancePointAndLine(vatthe.PosCenter, thanh.LineOfMPN()) <= ConstPhysic._R + 10 && thanh.Position.X <= vatthe.PosCenter.X && vatthe.PosCenter.X <= thanh.PointEnd().X)
            {
                return true;
            }
            return false;
        }
        public Vector2 LineChangeV()
        {
            for (int i = 0; i < _listThanh.Count; i++)
            {
                if (CheckCollisionMPN(_listThanh[i], _vatthe))
                {
                    if (_vatthe.V >= -10 && _vatthe.V < 0)
                    {
                        return _vatthe.PosCenter;
                    }
                }
            }
            return Vector2.Zero;
        }
        public int NumberOfLineChange()
        {
            for (int i = 0; i < _listThanh.Count; i++)
            {
                if (CheckCollisionMPN(_listThanh[i], _vatthe))
                {
                    if (_vatthe.V < 0 && _vatthe.V >= -10)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
        public void Draw(SpriteBatch sp)
        {
            Camera2 ca = new Camera2();
            float xOb, yOb;
            xOb = _vatthe.PosCenter.X;
            yOb = _vatthe.PosCenter.Y;
            ca.UpdateCamera(xOb, yOb);
            float x, y;
            x = xOb - ca.xcamera;
            y = yOb - ca.ycamera;
           
            Rectangle soure = new Rectangle(0, 0, 400, 4);
            Rectangle soure2 = new Rectangle(0, 0, 40, 15);
           

            for (int i = 0; i < _listThanh.Count && _listThanh[i] != null; i++)
            {

                if(_isPause==false)
                    _listThanhDraw[i].Position-= new Vector2(ca.xcamera,ca.ycamera);

                _listThanhDraw[i].Draw(sp);               
            }
            if (isDrawFly)
            {
                _PosGone.Add(new Vector2(_vatthe.PosCenter.X - 30, _vatthe.PosCenter.Y - 30));
            }
            for (int i = 0; i < _PosGone.Count; i++)
            {
                sp.Draw(picPosGone, new Rectangle((int)(_PosGone[i].X - ca.xcamera + 30), (int)(_PosGone[i].Y - ca.ycamera + 30), 10, 10), Color.White);
            }
            sp.Draw(_vatthe.Picture, new Rectangle((int)(x - ConstPhysic._R), (int)(y - ConstPhysic._R), 60, 60), Color.White);
            //sp.Draw(_vatthe.Picture, new Rectangle((int)(_vatthe.PosCenter.X - ConstPhysic._R), (int)(_vatthe.PosCenter.Y - ConstPhysic._R), 60, 60), Color.White);
            //draw lo xo
            sp.Draw(_vatthe2.Picture, new Rectangle((int)(_vatthe2.Pos.X-ca.xcamera), (int)(_vatthe2.Pos.Y-ca.ycamera), 60, 60), Color.White);
            sp.Draw(_imageSprig, new Rectangle(0-(int)ca.xcamera, (int)(_vatthe2.Pos.Y + 15-ca.ycamera), (int)(_vatthe2.Pos.X), 30), Color.White);            
            
            sp.Draw(picMatDat, new Rectangle(0, (int)(ConstPhysic.DoCaoMatDat - ca.ycamera), 10000, 10), Color.White);
            DrawButton(sp);
        }        
        public void DrawButton(SpriteBatch sp)
        {
            if (_isMOBPlay)
                sp.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                sp.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isMOBStop)
                sp.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                sp.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isMOBPause)
                sp.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                sp.Draw(picPause, ConstPhysic.btnPause, Color.White);           
        }
        public float DeltaAcceleration(ThanhMPN mpn)
        {
            float a;
            a = (float)(_vatthe.M * ConstPhysic.Gravity * Math.Sin(mpn.Angle) * 100);
            return a;
        }

        public void AddMPN(string gocnghieng, string chieudai, bool createForm)
        {
            int n = _listThanh.Count;
            double gocnghieng1 = double.Parse(gocnghieng);
            float chieudai1 = float.Parse(chieudai);
            ThanhMPN thanh = new ThanhMPN(game, gocnghieng1, chieudai1, _listThanh[_listThanh.Count - 1].PointEnd());
            _listThanh.Add(thanh);
            TimeGame._ShowForm = createForm;
        }
        public void SetValueShowForm(bool createForm)
        {
            TimeGame._ShowForm = createForm;
        }
        public void CreateForm()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl) && TimeGame._ShowForm && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                FrMPNDefault fr = new FrMPNDefault();
                fr.GetMyValue = new FrMPNDefault.GetString(SetValue);
                fr.GetMyValue2 = new FrMPNDefault.GetString2(SetValue2);
                fr.Show();
                TimeGame._ShowForm = false;
            }
        }
        public void MouseOverButton()
        {           
            if (mouse.CheckMouse(ConstPhysic.btnPause)) _isMOBPause = true;
            else _isMOBPause = false;
            if (mouse.CheckMouse(ConstPhysic.btnPlay)) _isMOBPlay = true;
            else _isMOBPlay = false;
            if (mouse.CheckMouse(ConstPhysic.btnStop)) _isMOBStop = true;
            else _isMOBStop = false;
        }
        public void CheckButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnPlay) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                _isPause = false;
            if (mouse.CheckMouse(ConstPhysic.btnPause) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                _isPause = true;
            if (mouse.CheckMouse(ConstPhysic.btnStop) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                _isPause = true;
                ReSet();
            }
        }

        private void ReSet()
        {
           // _PosGone.Clear();
            _vatthe.PosCenter = S0;
            for (int i = 0; i < 10; i++)
            {
                _listTime[i] = 0;
                _listV0[i] = 0;
                _listTimeBack[i] = 0;
                _listV0back[i] = 0;
                _GiaTocChange[i] = 0;
                _GiaTocChangeBack[i] = 0;
            }
            _GiaTocChange[0] = _giaTocBanDau;
            _listV0[0] = _vantocLast;
            _vatthe.V0 = _vantocLast;
            _vatthe.A = _GiaTocChange[0];
            _vatthe.V = 0;
            ////////////////////
            _vatthe2.AnBegin =_PhaBanDauCLLX;
            _vatthe2.AngFre = _TanSoGocCLLX;
            _vatthe2.Pos = S01;
            ///////////////
            TimeGame._gameTime2 = 0;

            _listThanhDraw[0].Position = _thanhMPN1.Position;
            _listThanhDraw[1].Position = _thanhMPN2.Position;
            _listThanhDraw[2].Position = _thanhMPN3.Position;
        }
        public void SetValue(string vantoc, string luc, string khoiluong, string hesomasat)
        {
            this._f = float.Parse(luc);
            this._heSoMaSat = float.Parse(hesomasat);
            float vantoc1 = float.Parse(vantoc);
            float luc1 = float.Parse(luc);
            float khoiluong1 = float.Parse(khoiluong);
            float hesomasat1 = float.Parse(hesomasat);
            float lucmasat = khoiluong1 * ConstPhysic.Gravity * hesomasat1;
            float luctonghop = luc1 - lucmasat;
            if (luctonghop < 0)
            {
                luctonghop = 0;
            }
            float giatoc1 = luctonghop / khoiluong1;
            _vatthe.M = khoiluong1;

            _vatthe.PosCenter = S0;
            for (int i = 0; i < 10; i++)
            {
                _listTime[i] = 0;
                _listV0[i] = 0;
                _listTimeBack[i] = 0;
                _listV0back[i] = 0;
                _GiaTocChange[i] = 0;
                _GiaTocChangeBack[i] = 0;
            }
            _GiaTocChange[0] = giatoc1;
            _listV0[0] = vantoc1;
            _vatthe.V0 = vantoc1;
            _vatthe.A = _GiaTocChange[0];
            _vatthe.V = 0;
        }
        public void SetValue2(bool CreateForm)
        {
            TimeGame._ShowForm = CreateForm;
        }
        //-----------------------------------------------------------------------------------------------------


        public Vector2 EquationMove()
        {
            return new Vector2((float)(Amplitude * Math.Cos(_vatthe2.AngFre * TimeGame._gameTime + _vatthe2.AnBegin)), 0) + S01;
        }

        public void MoveCLLX(GameTime gameTime)
        {
            if (_isPause==false)
            {
                TimeGame._gameTime += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                _vatthe2.Pos = EquationMove();
                _vatthe2.A = (float)(-1 * Math.Pow(_vatthe2.AnBegin, 2) * (EquationMove().X - S01.X));            
            }
                
        }
        public void DrawCLLX(SpriteBatch sp)
        {
           
        }
        public Rectangle getRectVOb2()
        {
            Rectangle a = new Rectangle();
            a= new Rectangle((int)_vatthe2.Pos.X, (int)_vatthe2.Pos.Y, 60, 60);
            return a;
        }
        public Rectangle getRectVOb1()
        {
             Rectangle a = new Rectangle();
            a= new  Rectangle((int)_vatthe.PosCenter.X-30, (int)_vatthe.PosCenter.Y-30, 60, 60);
            return a;
        }
        public bool CheckVaCham()
        {
            Rectangle A = getRectVOb1();
            Rectangle B = getRectVOb2();
            if (A.Intersects(B))
            {
                return true;
            }
            return false;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////
        public void ChuyenDongNemXien(GameTime gameTime,double phabandau,Vector2 vitribandau,float vantocbandau)
        {
            if (_isPause == false)
            {
                if (_vatthe.PosCenter.Y >= ConstPhysic.DoCaoMatDat-30)
                {
                    _isPause = true;
                }
                TimeGame._gameTime2 += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                float x = (float)((vantocbandau * Math.Cos(phabandau)) * TimeGame._gameTime2);
                float y = -(float)((vantocbandau * Math.Sin(phabandau)) * TimeGame._gameTime2 - 0.5 * ConstPhysic.Gravity * TimeGame._gameTime2 * TimeGame._gameTime2);                
                _vatthe.PosCenter = new Vector2(x, y) + vitribandau+new Vector2(0,-30);
            }

        }
    }
}
